#include "ZString.h"

ZString::ZString()
{
    z_string = NULL;
    z_length = 0;
}

ZString::ZString(const char *string)
{
    z_string = copy(string);
    z_length = length(string);
}

ZString::ZString(const ZString &string)
{
    z_string = copy(string.z_string);
    z_length = string.z_length;
}


ZString ZString::operator=(const char *string)
{
    delete[] z_string;
    z_string = copy(string);
    z_length = length(string);

    return *this;
}

ZString ZString::operator=(const ZString &string)
{
    delete[] z_string;
    z_string = copy(string.z_string);
    z_length = string.z_length;

    return *this;
}

ZString ZString::operator+(const char *string)
{
    int totalSize = z_length + longueur(string);
    char *stringSum = new char[totalSize + 1];

    for (int i = 0 ; i < z_length ; i++)
    {
        stringSum[i] = z_string[i];
    }

    for (int i = z_length ; i < totalSize ; i++)
    {
        stringSum[i] = string[i - z_length];
    }
    stringSum[totalSize] = '\0';

    ZString resultat(stringSum);
    delete[] stringSum;
    return resultat;
}

ZString ZString::operator+(const ZString &string)
{
    int totalSize = z_length + string.z_length;
    char *stringSum = new char[totalSize + 1];

    for (int i = 0 ; i < z_length ; i++)
    {
        stringSum[i] = z_string[i];
    }

    for (int i = z_length ; i < totalSize ; i++)
    {
        stringSum[i] = string.z_string[i - z_length];
    }
    stringSum[totalSize] = '\0';

    ZString resultat(stringSum);
    delete[] stringSum;
    return result;
}

bool ZString::operator==(const char *string) const
{
	if(z_string == string && z_length == length(string))
		return true;
	else
		return false;
}

bool ZString::operator==(const ZString &string) const
{
	if(z_string == string.z_string && z_length == string.z_length)
		return true;
	else
		return false;
}

bool ZString::operator!=(const char *string) const
{
	if(z_string != string || z_length != length(string))
		return true;
	else
		return false;
}

bool ZString::operator!=(const ZString &string) const
{
	if(z_string != string.z_string || z_length != string.z_length)
		return true;
	else
		return false;
}

char ZString::operator[](const int index)
{
	return z_string[index];
}

char *ZString::getString() const
{
    return z_string;
}

int ZString::getLength() const
{
    return z_length;
}

int ZString::length(const char *string)
{
    int i = 0;

    while (string[i] != '\0')
    {
        i++;
    }

    return i;
}

char *ZString::copy(const char *string)
{
    int size = length(string);
    char *stringCopied = new char[size + 1];


    for (int i = 0 ; i < taille ; i++)
    {
        stringCopied[i] = string[i];
    }
    stringCopied[size] = '\0';

    return stringCopied;
}


ZString::~ZString()
{
    delete[] z_string;
}
